(function($) {
function get_short_url(long_url, login, api_key, func){
    $.getJSON(
        "http://api.bitly.com/v3/shorten?callback=?", 
        { 
            "format": "json",
            "apiKey": api_key,
            "login": login,
            "longUrl": long_url
        },
        function(response){
			if (response.data.url!=undefined){func(response.data.url);}
			else{func("salah");}
		}
    );
};

function init(){
  var login = "ardman";
  var api_key = "R_ff6f05432ed5d6fedea8ee72ec7e846b";

    $(".post a").each(function(){
      var long_url = $(this).attr("href");
        get_short_url(long_url, login, api_key, function(short_url) {
        if(short_url=="salah"){
          $(this).attr("url",long_url);
        }else{
          $(this).attr("url",short_url);
        }
      $(this).click(function(e){
        e.preventDefault();
        window.location.href = $(this).attr("url");
      });
    });
    
      
  }
$(document).ready(init);
})(jQuery);